#!/bin/bash
# clone.sh 
# 可以用来克隆 ems 项目的源代码

set -e

##################### 获取工作目录 #####################
# 参考：http://www.gimoo.net/t/1502/54ea928e14fef.html
SOURCE="$0"
while [ -h "$SOURCE"  ]; do                              # resolve $SOURCE until the file is no longer a symlink
    DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /*  ]] && SOURCE="$DIR/$SOURCE"        # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE"  )" && cd .. && pwd  )"
#######################################################

app=$(basename $DIR)     # app="ems"
workdir=$DIR             # workdir="/opt/$app"

# 参数：<模块>，可能的值：
#   biz, etl, mock, designer, biz-ui, designer-ui
module='archive'
branch='develop'
src_module="ems-archive"

# 使用说明
usage() {
    echo -e "-------------------------------------------------------"
    echo -e "HELP: 用法: ./clone.sh"
    exit 1
}

echo "INFO: [$module] 主机：$(hostname) 分支：$branch"

# 创建目录
if [ ! -d "$workdir/src" ]; then
  echo "INFO: [$module] 创建目录：$workdir/src"
  mkdir -p "$workdir/src"
fi

# 克隆
clone(){
  echo "INFO: [$module] 拉取 $branch 分支代码到 $workdir/src/$src_module"

  echo "INFO: [$module] 删除之前克隆的代码..."
  cd "$workdir/src"
  rm -rf $src_module

  echo "INFO: [$module] 正在克隆代码：git clone -b $branch git@gitee.com:caibaohong/$src_module.git"
  git clone -b "$branch" "git@gitee.com:caibaohong/$src_module.git"
}

clone